<?php

if ( ! class_exists("MyLotto_3d") )
{
    include_once( "class_3d/MyLotto_3d.php" );
}

class MyLotto_sb extends MyLotto_3d
{

    public function copyRateZeroToN($lotto_3d_id)
    {
        global $g_gametype_3d;
        $table_name = "rate_".$g_gametype_3d[$this->gametype]['en'];
        $sql = "INSERT INTO {$table_name} (lotto_id, bettype,subid, disk, r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17)";
        $sql .= " SELECT {$lotto_3d_id} AS lotto_id, bettype,subid, disk, r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17";
        $sql .= " FROM {$table_name} WHERE lotto_id=0";
		return $this->db->query($sql);
    }

    public function checkOpen($n1 , $n2 , $n3)
    {
        return true;
    }

    public function checkNum($n)
    {
        if ( $n == "" )
        {
            return true;
        }
        if ( 0 - 1 <= $n && $n < 7 )
        {
            return true;
        }
        return false;
    }

    public function showText($n , $type)
    {
        $bsArr = array ( 0 => "豹子" , 1 => "<font class=text01>大</font>" , 2 => "<font class=text02>小</font>" );
        $reVal = "";
        switch ( $type )
        {
            case "bs" :
                $reVal = $bsArr[$n];
                break;
            default :
                $reVal = $n;
                break;
        }
        return $reVal;
    }

    public function calOpenNum($n3 , $n2 , $n1 , $n4=-1 , $n5=-1 , $n6=-1 , $n7=-1 , $n8=-1 , $n9=-1 , $n10=-1)
    {
        return $this->calOpenNum3N($n3 , $n2 , $n1);
    }

    public function calOpenNum3N($n1 , $n2 , $n3)
    {
        $updateData = array ( );
        if ( $this->checkOpen($n1 , $n2 , $n3) )
        {
			$total=$n1+$n2+$n3;
			$arr=array(3,5,7,9,11,13,15,17);
			$updateData['sb']=in_array($total,$arr)?1:2;
			$updateData['dz']=0;
			if(!($n1==$n2 && $n2==$n3))
				$updateData['bs']=($total>=11?1:2);
			else
			{
				$updateData['bs']=0;
				$updateData['bz']=$n1;
			}
			$arr1=array_count_values(array($n1 , $n2 , $n3));
			foreach($arr1 as  $key=>$val)
			{
				if($val==2)
				{
					$updateData['dz']=$key;
					break;	
				}
			}
        }

        return $updateData;
    }

    public function getLottosbByDateTime()
    {
		$sql = "SELECT * FROM {$this->table_name} WHERE ('".date("Y-m-d H:i:s" , systime())."' BETWEEN startbet AND stopbet) AND ((n3 + n2 + n1) = -3) limit 0,1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

    /* 下一期时间 */

    public function getLottoNextDateTime()
    {
        $n = "((n3 + n2 + n1) = -3)";
        $sql = "SELECT time_to_sec(timediff(startbet,sysdate())) as sec FROM {$this->table_name} WHERE startbet > sysdate() AND {$n} ORDER BY startbet ASC limit 0,1";
		$result = $this->db->query($sql);
        unset($sql);
        return mysql_fetch_array($result , MYSQL_ASSOC);
    }

}